//导入express模块
const express = require("express")
//开启路由
const router = express.Router()
//格式化时间
var dayjs = require("dayjs")
//导入方法
const { getAll, getOne, exec } = require("../db")

/* 查询所有事件 */
router.get("/gg_event", async (req, res) => {
  //1、解析请求的参数
  //2、编写sql语句
  /* 只查询未处理的事件 */
  let sql = `select * from gg_event where event_status = 0`
  //3、执行sql语句返回数据
  try {
    const data = await getAll(sql)
    res.send({
      code: 0,
      mag: "查询所有事件成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 000001,
      msg: "查询所有事件失败",
      result: debug ? error : "",
    })
  }
})
/* 查询最新审核事件 */
router.get("/gg_event/audit", async (req, res) => {
  //1、解析请求的参数
  //2、编写sql语句
  /* 只查询未处理已审核的事件 */
  let sql = `select * from gg_event where event_status = 2 order by event_time desc limit 1`
  //3、执行sql语句返回数据
  try {
    const data = await getAll(sql)
    res.send({
      code: 0,
      mag: "查询所有事件成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 000001,
      msg: "查询所有事件失败",
      result: debug ? error : "",
    })
  }
})

/* 按类型\时间查询 */
router.get("/gg_event_typy_time", async (req, res) => {
  //1.解析请求的参数
  const key = Object.keys(req.body)
  const value = Object.values(req.body)
  //3.执行sql语句，返回数据
  try {
    if (key == "event_type") {
      //2.编写sql语句
      let sql = `SELECT * FROM gg_event WHERE ${key}='${value}'`
      // console.log(sql)
      //3、执行sql语句，返回数据
      const data = await getAll(sql)
      res.send({
        code: 0,
        msg: "按类型查询成功",
        result: data,
      })
    } else if (key == "event_time") {
      //2.编写sql语句
      let sql = `SELECT * FROM gg_event WHERE ${key} like '${value}'`
      // console.log(sql)
      //3、执行sql语句，返回数据
      const data = await getAll(sql)
      res.send({
        code: 0,
        msg: "按时间查询成功",
        result: data,
      })
    }
  } catch (error) {
    res.send({
      code: 000006,
      msg: "按类型、时间查询失败",
      result: debug ? error : "",
    })
  }
})

/* 添加事件 */
router.post("/gg_event", async function (req, res) {
  //1、解析请求数据
  const {
    user_id,
    event_type,
    event_addr,
    event_mark,
    event_time,
    event_descibe,
    event_status,
  } = req.body
  //2、编写sql语句
  let sql = `INSERT INTO gg_event(user_id,event_type,event_addr,event_mark,event_time,event_descibe,event_status) VALUES ('${user_id}','${event_type}','${event_addr}','${event_mark}','${event_time}','${event_descibe}','${event_status}')`
  //3、执行sql语句，返回数据
  try {
    const data = await exec(sql)
    res.send({
      code: 0,
      msg: "添加事件成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 0,
      msg: "添加事件失败",
      result: debug ? error : "",
    })
  }
})

/* 修改事件状态 */
router.put("/gg_event", async (req, res) => {
  //1、解析请求参数
  const { event_id, user_id, event_status } = req.body
  //2、编写sql语句
  //3、执行sql语句，返回数据
  try {
    let sql = `select user_type from gg_user where user_id='${user_id}'`
    const data = await getAll(sql)
    const usetType = Object.values(data[0])
    console.log(usetType)
    if (true) {
      const sql = `update gg_event set event_status=${event_status} where event_id=${event_id}`
      const data = await getAll(sql)
      // console.log("交管", sql)
      res.send({
        code: 0,
        msg: "修改事件状态成功",
        result: data,
      })
    } else {
      // console.log("没有权限")
      res.send({
        code: 0,
        msg: "没有权限",
        result: data,
      })
    }
  } catch (error) {
    res.send({
      code: 000004,
      msg: "没有权限",
      result: debug ? error : "",
    })
  }
})

module.exports = router
